package com.example.fengyungo.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.example.fengyungo.MyWebSocketClient;
import com.example.fengyungo.constant.DriverConstant;
import com.example.fengyungo.constant.MessageConstant;
import com.example.fengyungo.utils.DriverUtils;
import com.example.fengyungo.utils.SharedPreferencesUtils;

import okhttp3.WebSocket;

/**
 * 进行websocket的一些连接等操作
 * @author 徐
 */
public class WebSocketService extends Service {

    private static final String TAG = "WebSocketService";

    private Binder mBinder = new WebSocketClientBinder();

    public MyWebSocketClient myWebSocketClient;
    /**
     * 使用本地广播
     */
    private LocalBroadcastManager localBroadcastManager;
    private Context context;

    public class WebSocketClientBinder extends Binder {
        public WebSocketService getService() {
            return WebSocketService.this;
        }
    }

    public WebSocketService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /**
     * 服务创建时调用
     */
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: WebSocketService");

    }

    void initMyWebSocketClient() {
        String url="ws:"+ DriverConstant.NGROK_URL_FENGYUNGO.substring(6) +"/ws/";
//        String url="ws://192.168.137.1:8080/ws/";
        Log.d(TAG, "initMyWebSocketClient: url="+url);
        String driverId = SharedPreferencesUtils.getDriverInfoInService(this).getId();
        myWebSocketClient = new MyWebSocketClient(url,driverId) {
            @Override
            public void onMessage(@NonNull WebSocket webSocket, @NonNull String text) {
                Log.d(TAG, "onMessage:");
                Log.d(TAG, "Received message: " + text);
                if ("pong".equals(text)) {
                    // 接收到服务器的确认消息，更新最后接收到心跳的时间
                    lastPingTime = System.currentTimeMillis();
                } else {
                    // 处理接收到的其他消息
                    localBroadcastManager = LocalBroadcastManager.getInstance(context);//获取实例
                    // 发送广播到ChatActivity
                    Intent intent = new Intent();
                    intent.setAction("webSocketService");
                    intent.putExtra(MessageConstant.MESSAGE, text);
                    // 发送本地广播
                    localBroadcastManager.sendBroadcast(intent);
                }
            }
        };
        myWebSocketClient.connect();
    }

    /**
     * 每次启动服务时调用
     * @param intent Intent
     * @param flags int
     * @param startId int
     * @return int
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: WebSocketService");
        context = this;
        initMyWebSocketClient();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: WebSocketService");
        // 关闭WebSocket连接
        myWebSocketClient.close();
    }
}